unit ORM.ConsultClientDataSet;

interface

uses
  Datasnap.DBClient, Data.DBXCommon;

type
  TConsultClientDataSet = class
  private
    class function DBXReaderFromClientDataSet(const pReader: TDBXReader ):TClientDataSet; static;
    class procedure GetCamposClientDatSet(const pReader : TDBXReader; pClientDataSet: TClientDataSet);  static;
    class procedure GetValuesClientDataSet(const pReader : TDBXReader; pClientDataSet: TClientDataSet);  static;
  public
    class function LoadClientDataSet( const pObject : TObject ) : TClientDataSet; overload;
    class function LoadClientDataSet( const pObject : TObject; pFilter: string): TClientDataSet; overload;
    class function LoadClientDataSet( const pSQLCommand : string; pFilter : String): TClientDataSet; Overload;
    class function LoadClienteDataSetForAnyField( const pObject : TObject) : TClientDataSet;
  end;

implementation

uses
  DB, System.Rtti, System.SysUtils, ORM.Attributes, TypInfo, ORM.Drive.Connection,
  ORM.Base, ORM.Utils, ORM.SQLCommands;

{ TConsultClientDataSet }

class function TConsultClientDataSet.DBXReaderFromClientDataSet(const pReader: TDBXReader): TClientDataSet;
var
   ClientDataSet : TClientDataSet;
begin
  ClientDataSet := TClientDataSet.Create(nil);
  GetCamposClientDatSet(pReader, ClientDataSet);
  GetValuesClientDataSet(pReader, ClientDataSet);
  Result := ClientDataSet;
end;

class function TConsultClientDataSet.LoadClienteDataSetForAnyField(const pObject: TObject): TClientDataSet;
var
  DBXCommand: TDBXCommand;
  DBXReader: TDBXReader;
begin
  try
    try
      DBXCommand :=  TConnectionDBX.GetConnection.DBXConnection.CreateCommand;
      DBXCommand.Text := TSQLCommands.StringConsult(pObject);
      DBXCommand.Prepare;
      DBXReader := DBXCommand.ExecuteQuery;
      result := DBXReaderFromClientDataSet(DBXReader);
    except
      result := nil;
    end;
  finally
    FreeAndNil(DBXCommand);
    FreeAndNil(DBXReader);
  end;
end;

class function TConsultClientDataSet.LoadClientDataSet(const pObject: TObject; pFilter: string): TClientDataSet;
var
  DBXCommand : TDBXCommand;
  DBXReader : TDBXReader;
begin
  try
    try
      DBXCommand := TConnectionDBX.GetConnection.DBXConnection.CreateCommand;
      DBXCommand.Text := TSQLCommands.StringConsult(pObject, pFilter, '');
      DBXCommand.Prepare;
      DBXReader := DBXCommand.ExecuteQuery;
      result := DBXReaderFromClientDataSet(DBXReader);
    except
      result := nil;
    end;
  finally
    FreeAndNil(DBXCommand);
    FreeAndNil(DBXReader);
  end;
end;

class function TConsultClientDataSet.LoadClientDataSet(const pSQLCommand : String; pFilter: String): TClientDataSet;
var
  FiltroSQL: String;
  DBXCommand: TDBXCommand;
  DBXReader : TDBXReader;
begin
  try
    try
      if pFilter <> '' then
      begin
        pFilter := FormataFiltro(pFilter);
        FiltroSQL := ' WHERE ' + pFilter;
      end;
      DBXCommand := TConnectionDBX.GetConnection.DBXConnection.CreateCommand;
      DBXCommand.Text := pSQLCommand + FiltroSQL;
      DBXCommand.Prepare;
      DBXReader := DBXCommand.ExecuteQuery;
      result := DBXReaderFromClientDataSet(DBXReader);
    except
      result := nil;
    end;
  finally
    FreeAndNil(DBXCommand);
    FreeAndNil(DBXReader);
  end;
end;

class function TConsultClientDataSet.LoadClientDataSet(const pObject: TObject): TClientDataSet;
var
  DBXCommand: TDBXCommand;
  DBXReader : TDBXReader;
begin
  try
    try
      DBXCommand := TConnectionDBX.GetConnection.DBXConnection.CreateCommand;
      DBXCommand.Text := TSQLCommands.StringConsult(pObject, '', '');
      DBXCommand.Prepare;
      DBXReader := DBXCommand.ExecuteQuery;
      result := DBXReaderFromClientDataSet(DBXReader);
    except
      result := nil;
    end;
  finally
    FreeAndNil(DBXReader);
    FreeAndNil(DBXCommand);
  end;
end;

class procedure TConsultClientDataSet.GetValuesClientDataSet(const pReader : TDBXReader; pClientDataSet: TClientDataSet);
var
  DBXValueType: TDBXValueType;
  j: Integer;
  NomeCampo: string;
  I: Integer;
begin
  pClientDataSet.CreateDataSet;
  pClientDataSet.Open;
  while pReader.Next do
  begin
    pClientDataSet.Append;
    for i := 0 to pClientDataSet.FieldCount - 1 do
    begin
      for j := 0 to pReader.ColumnCount - 1 do
      begin
        DBXValueType := pReader.ValueType[j];
        NomeCampo := DBXValueType.Name;
        if pClientDataSet.Fields[i].FieldName = pReader.ValueType[j].Name then
        begin
          case DBXValueType.DataType of
            TDBXDataTypes.UnknownType: pClientDataSet.Fields[i].AsString := '';
            TDBXDataTypes.AnsiStringType: pClientDataSet.Fields[i].Value := pReader.Value[j].GetAnsiString;
            TDBXDataTypes.DateType: pClientDataSet.Fields[i].AsDateTime := pReader.Value[j].GetDate;
            TDBXDataTypes.BlobType: pClientDataSet.Fields[i].Value := pReader.Value[j].AsString;
            TDBXDataTypes.BooleanType: pClientDataSet.Fields[I].Value := pReader.Value[J].AsBoolean;
            TDBXDataTypes.Int16Type: pClientDataSet.Fields[i].Value := pReader.Value[j].GetInt16;
            TDBXDataTypes.Int32Type: pClientDataSet.Fields[i].Value := pReader.Value[j].GetInt32;
            TDBXDataTypes.DoubleType: pClientDataSet.Fields[i].Value := pReader.Value[j].GetDouble;
            TDBXDataTypes.BcdType: pClientDataSet.Fields[i].AsBCD := pReader.Value[j].AsBcd;
            TDBXDataTypes.TimeType: pClientDataSet.Fields[i].Value := pReader.Value[j].GetTime;
            TDBXDataTypes.DateTimeType: pClientDataSet.Fields[i].Value := pReader.Value[j].AsDateTime;
            TDBXDataTypes.UInt16Type: pClientDataSet.Fields[i].Value := pReader.Value[j].GetUInt16;
            TDBXDataTypes.Uint32Type: pClientDataSet.Fields[i].Value := pReader.Value[j].ASInt32;
            TDBXDataTypes.Int64Type: pClientDataSet.Fields[i].Value := pReader.Value[j].GetInt64;
            TDBXDataTypes.Uint64Type: pClientDataSet.Fields[i].Value := pReader.Value[j].GetInt64;
            TDBXDataTypes.TimeStampType: pClientDataSet.Fields[i].AsDateTime := pReader.Value[j].AsDateTime;
            TDBXDataTypes.CurrencyType: pClientDataSet.Fields[i].Value := pReader.Value[j].AsDouble;
            TDBXDataTypes.WideStringType: pClientDataSet.Fields[i].Value := pReader.Value[j].GetWideString;
            TDBXDataTypes.SingleType: pClientDataSet.Fields[I].Value := pReader.Value[J].AsSingle;
            TDBXDataTypes.Int8Type: pClientDataSet.Fields[i].Value := pReader.Value[j].GetInt8;
            TDBXDataTypes.Uint8Type: pClientDataSet.Fields[i].Value := pReader.Value[j].GetUInt8;
            TDBXDataTypes.VariantType: pClientDataSet.Fields[i].Value := pReader.Value[j].AsVariant;
          end;
          break;
        end;
      end;
    end;
    pClientDataSet.Post;
  end;
end;

class procedure TConsultClientDataSet.GetCamposClientDatSet(const pReader : TDBXReader; pClientDataSet: TClientDataSet);
var
  NomeCampo: string;
  I: Integer;
  DBXValueType: TDBXValueType;
begin
  for i := 0 to pReader.ColumnCount - 1 do
  begin
    DBXValueType := pReader.ValueType[I];
    NomeCampo := DBXValueType.Name;
    case DBXValueType.DataType of
      TDBXDataTypes.AnsiStringType, TDBXDataTypes.WideStringType:
        pClientDataSet.FieldDefs.Add(NomeCampo, ftString, 200, False);
      TDBXDataTypes.BlobType:
        pClientDataSet.FieldDefs.Add(NomeCampo, ftString, 300, False);
      TDBXDataTypes.DateType, TDBXDataTypes.DateTimeType, TDBXDataTypes.TimeStampType:
        pClientDataSet.FieldDefs.Add(NomeCampo, ftDateTime);
      TDBXDataTypes.TimeType:
        pClientDataSet.FieldDefs.Add(NomeCampo, ftTime);
      TDBXDataTypes.Int8Type, TDBXDataTypes.Int16Type, TDBXDataTypes.Int32Type, TDBXDataTypes.Int64Type,
      TDBXDataTypes.UInt8Type, TDBXDataTypes.UInt16Type:
        pClientDataSet.FieldDefs.Add(NomeCampo, ftInteger);
      TDBXDataTypes.DoubleType, TDBXDataTypes.CurrencyType, TDBXDataTypes.BcdType, TDBXDataTypes.SingleType:
        pClientDataSet.FieldDefs.Add(NomeCampo, ftFloat);
      TDBXDataTypes.BinaryBlobType, TDBXDataTypes.BytesType, TDBXDataTypes.VariantType:
        pClientDataSet.FieldDefs.Add(NomeCampo, ftVariant);
      TDBXDataTypes.BooleanType: pClientDataSet.FieldDefs.Add(NomeCampo, ftBoolean);
    end;
  end;
end;



end.
